#include "musicslider.h"
#include "ui_musicslider.h"

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider),
    currentPos(0)
{
    ui->setupUi(this);
    moveSlider();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSlider();
    emit setMusicSilderPosition(currentPos/(float)width());
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    //需要限制滑动的范围
    QRect rect = QRect(0,0,width(),height());
    QPoint pos = event->pos(); //鼠标的位置
    if(!rect.contains(pos))
    {
        return;
    }

    if(event->buttons() == Qt::LeftButton)
    {
        currentPos = event->pos().x();

        if(currentPos<0)
            currentPos = 0;
        else if(currentPos>width())
            currentPos = width();
        moveSlider();
    }
}

void MusicSlider::setStep(float bf)
{
    currentPos = width()*bf;
    moveSlider();
}

void MusicSlider::moveSlider()
{
    ui->outLine->setMaximumWidth(currentPos);
    ui->outLine->setGeometry(QRect(ui->outLine->geometry().x(),ui->outLine->geometry().y(),currentPos,ui->outLine->height()));
}
